<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <div class="layui-tab-content">
                <form class="layui-form" style="width:100%">
                    <div class="layui-form-item">
                        <label class="layui-form-label">消息标题</label>
                        <div class="layui-input-block">
                            <input type="text" id="title" name="title" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <input type="radio" name="status" value="1" title="显示">
                                <input type="radio" name="status" value="0" title="隐藏">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label ">消息内容</label>
                        <div class="layui-input-block">
                            <textarea id="mesContent" name="message" placeholder="请输入内容" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-hide xxpayYesBtn" lay-submit lay-filter="upBtn">保存</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form','table', 'admin', 'util', 'layedit'], function(){
        var form = layui.form
        ,table = layui.table
        ,$ = layui.$
        ,admin = layui.admin
        ,view = layui.view
        ,layedit = layui.layedit
        ,element = layui.element;
        var index;
        element.render('breadcrumb', 'breadcrumb');//渲染导航信息
        var id = view.getOpenParams('id');
        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/sys/message/get',
            data: {id: id},
            success: function(res){
                if(res.code == 0){
                    $('#title').val(res.data.title);
                    if(res.data.status == 1) {
                        $("input[name='status'][value='1']").attr("checked", true);
                    }else {
                        $("input[name='status'][value='0']").attr("checked", true);
                    }
                    $('#mesContent').val(res.data.message);
                    index = layedit.build('mesContent'); //建立编辑器
                    form.render();
                }else {
                    layer.alert(res.msg,{
                        title: '请求成功，但是状态不对'
                    })
                }
            }
        });

        form.on('submit(upBtn)', function(data){
            // console.log(layedit.getContent(index));
            admin.req({
                type: 'POST',
                url: layui.setter.baseUrl + '/sys/message/update',
                data: {
                    id: id,
                    title: data.field.title,
                    status: data.field.status,
                    message: layedit.getContent(index),
                },
                success: function(res){
                    if(res.code == 0){
                        layer.alert(res.msg,{title: '更新成功'},function(index){
                            layer.closeAll(); //关闭所有弹层
                            layui.table.reload('tableReload'); //调用业务弹层外表格重新加载
                        })
                    }else {
                        layer.alert(res.msg,{
                            title: '更新失败'
                        })
                    }
                }
            });
            return false;
        });

        form.render();
    })
</script>